//  随机推荐
$.ajax({
type: 'get',
url: '/posts/random',
success: function(response) {
  let  randomTpl = `
    {{each data}}
    <li><a href="detail.html?id={{$value._id}}">
    <div class="randowContent">
      <p>{{$value.title}}</p>
      <span>阅读({{$value.meta.views}})</span>
    </div>
    <div class="randowImg">
      <img src="{{$value.thumbnail}}" alt="">
    </div>
    </a></li>
    {{/each}}
  ` 
  let html = template.render(randomTpl, {data: response})
  $('#randomBox').html(html)
}

})
// 最新评论
$.ajax({
  type: 'get',
  url: '/comments/lasted',
  success: function(response) {
let commentLastedTpl = `
{{each data}}
<li><a href="javascript:;">
<div class="avatarComment">
  <img src="{{$value.author.avatar}}" alt="">
</div>
<div class="commentContent">
  <p>{{$value.author.nickName}}  <i>{{$value.createAt.split('T')[0]}}说:</i></p>
  <span>{{$value.content}}</span>
</div>
</a></li>
{{/each}}
`
let html = template.render(commentLastedTpl, {data: response})
  $('#commentLasted').html(html)
  }
})
// 查询分类
$.ajax({
  type: 'get',
  url: '/categories',
  success: function(response) {
    let asideTpl = `
      {{each data}}
      <li><a href="list.html?id={{$value._id}}">
              <i class="iconfont {{$value.className}}
              "></i>{{$value.title}}
            </a></li>
      {{/each}}
    `
    let html = template.render(asideTpl, {data: response})
    $('.navAside').html(html)
  }
})
// 点赞功能
$('.article').on('click','#good', function() {
  var id = $(this).attr('data-id')
  let _this = $(this)
  $.ajax({
    type: 'post',
    url: '/posts/fabulous/'+ id,
    success: function(response) {
     _this.find('a').addClass('current')
    }
  })
})
// 获取id
function getId() {
  let num = location.search.indexOf('=')
  let id = location.search.substr(num+1)
  return id
}
$('.btn').on('click', function() {
  let key = $(this).prev().val()
  location.href = 'search.html?id='+ key
})
// 图片懒加载
lazyLoadInit({
  coverColor:"white",
  coverDiv:"<h1>test</h1>",
  offsetBottom:0,
  offsetTopm:0,
  showTime:1100,
  onLoadBackEnd:function(i,e){
      console.log("onLoadBackEnd:"+i);
  }
  ,onLoadBackStart:function(i,e){
      console.log("onLoadBackStart:"+i);
  }
})